﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Parse;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for ParseRelatedFiles.xaml
    /// </summary>
    public partial class ParseRelatedFiles : UserControl
    {
        public ParseRelatedFiles()
        {
            InitializeComponent();
        }

        private void FileBorder_PreviewDragEnter(object sender, DragEventArgs e)
        {
            bool isCorrect = true;

            if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
            {
                string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                foreach (string filename in filenames)
                {
                    if (File.Exists(filename) == false)
                    {
                        isCorrect = false;
                        break;
                    }
                    //FileInfo info = new FileInfo(filename);
                    //if (info.Extension != ".txt")
                    //{
                    //    isCorrect = false;
                    //    break;
                    //}
                }
            }
            if (isCorrect == true)
                e.Effects = DragDropEffects.All;
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }
        private void FileBorder_PreviewDrop(object sender, DragEventArgs e)
        {
            Guid classGUID = Guid.Empty;

            // Get Context 
            ParseDataContext ctx = ((HostWindow)Application.Current.MainWindow).DataContext as ParseDataContext;
            if (ctx != null)
            {
                if (ctx.SelectedClass != null)
                {
                    classGUID = ((ParseClass)ctx.SelectedClass).GUID;
                }
            }

            // Check 
            if (ctx == null)
                return;

            ParseApp _app = ctx.SelectedApp as ParseApp;
            if (_app == null)
                return;

            ParseObjectMask _parent = ctx.SelectedItem as ParseObjectMask;
            if (_parent == null)
                return;

            if (classGUID == Guid.Empty)
                return;

            string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
            if (filenames.Length >= 1)
            {
                foreach (string file in filenames)
                {
                    if (!File.Exists(file))
                        continue;

                    byte[] arr = File.ReadAllBytes(file);

                    ParseObjectMask.AttachFileToParent(_parent, arr, file, _app);

                    Task.Delay(1000);
                }
            }

            ParseOne.InformationSending(new ParseCustomEventArgs("RelatedFiles:Reload"));

            e.Handled = true;
        }
    }
}
